/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bc;

import edu.upn.sigemfi.ope.bc.BovedaFacadeLocal;
import edu.upn.sigemfi.ope.be.Boveda;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Paulo
 */
public class BovedaDelegate {

    private BovedaFacadeLocal local;

    public BovedaDelegate() {
        this.local = lookupBovedaFacade();
    }

    public void crear(Boveda boveda) throws Exception {
        try {
            local.create(boveda);
        } catch (Exception ex) {
            local = lookupBovedaFacade();
            throw new Exception("Crear Boveda... " + ex.getMessage());
        }
    }

    public void editar(Boveda boveda) throws Exception {
        try {
            local.edit(boveda);
        } catch (Exception ex) {
            local = lookupBovedaFacade();
            throw new Exception("Editar Boveda... " + ex.getMessage());
        }
    }

    public void eliminar(Boveda boveda) throws Exception {
        try {
            local.remove(boveda);
        } catch (Exception ex) {
            local = lookupBovedaFacade();
            throw new Exception("Eliminar Boveda... " + ex.getMessage());
        }
    }

    public List<Boveda> getBovedas() throws Exception {
        List<Boveda> bovedas;
        try {
            bovedas = local.findAll();
        } catch (Exception ex) {
            local = lookupBovedaFacade();
            throw new Exception("Listar Bovedas... " + ex.getMessage());
        }
        return bovedas;
    }

    public Boveda buscarPorId(Long id) throws Exception {
        Boveda boveda = null;
        try {
            boveda = local.find(id);
        } catch (Exception ex) {
            local = lookupBovedaFacade();
            throw new Exception("Buscar Boveda... " + ex.getMessage());
        }
        return boveda;
    }

    private BovedaFacadeLocal lookupBovedaFacade() {
        try {
            Context c = new InitialContext();
            return (BovedaFacadeLocal) c.lookup("java:comp/env/BovedaFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
